package com.example.basic.web.service.pharmStock.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.web.mapper.pharmStock.PharmStockMapper;
import com.example.basic.web.model.pharmStock.PharmStock;
import com.example.basic.web.response.PharmStockResponse;
import com.example.basic.web.response.report.StockCountResponse;
import com.example.basic.web.service.dataCode.DataCodeService;
import com.example.basic.web.service.pharmStock.PharmStockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

import static com.example.basic.utils.CheckUtil.notNull;


@Service
public class PharmStockServiceImpl extends BaseEntityServiceImpl<PharmStockMapper, PharmStock> implements PharmStockService {

    @Resource
    private PharmStockMapper pharmStockMapper;

    @Resource
    private DataCodeService dataCodeService;

    /**
     *
     * 库存列表
     * @param queryPage  分页
     * @param searchParam 模糊搜索(遗体名称，拼音码，五笔码)
     * @param sex 性别 0-全部
     * @param pharmCode 库房编码 0-全部
     * @param zoneCode 分区编码 0-全部
     * @param depotCode 库位编码 0-全部
     * @param status 状态 0-全部 1-占用，2-空闲
     * @return
     */

    @Override
    public IPage<PharmStockResponse> queryList(Page<PharmStockResponse> queryPage, String searchParam, String sex, String pharmCode, String zoneCode, String depotCode, String status) {
        return this.mapper.queryList(queryPage,searchParam,sex,pharmCode,zoneCode,depotCode,status);
    }

    /**
     * 待出库列表
     * @param queryPage  分页
     * @param searchParam 模糊搜索(遗体名称，拼音码，五笔码)
     * @param sex 性别 0-全部
     * @param beginTime 开始日期
     * @param endTime 结束日期
     * @return
     */
    @Override
    public IPage<PharmStockResponse> waitOutList(Page<PharmStockResponse> queryPage, String searchParam, String sex, String beginTime, String endTime) {
        return this.mapper.waitOutList(queryPage,searchParam,sex,beginTime,endTime);
    }

    /**
     * 校验库存是否使用
     * @param pharmCode 仓库编码
     * @param zoneCode 分区编码
     * @param depotCode 库位编码
     * @return
     */
    @Override
    public PharmStock judgeStock(String pharmCode, String zoneCode, String depotCode) {
        notNull(pharmCode,"仓库编码不能为空");
        notNull(zoneCode,"分区编码不能为空");
        notNull(depotCode,"库位编码不能为空");

        LambdaQueryWrapper<PharmStock> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PharmStock::getPharmCode,pharmCode);
        lqw.eq(PharmStock::getZoneCode,zoneCode);
        lqw.eq(PharmStock::getDepotCode,depotCode);

        return this.mapper.selectOne(lqw);
    }

    /**
     * 查询遗体库存信息
     * @param remainsCode 遗体编码
     * @return
     */
    @Override
    public PharmStock queryByRemainsCode(String remainsCode) {
        notNull(remainsCode,"遗体编码不能为空");

        LambdaQueryWrapper<PharmStock> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PharmStock::getRemainsCode,remainsCode);

        return this.mapper.selectOne(lqw);
    }

    /**
     * 库存信息查询
     * @param queryPage 分页
     * @param searchParam 模糊搜索(遗体名称，拼音码，五笔码)
     * @param pharmCode 库房编码 0-全部
     * @param zoneCode 分区编码 0-全部
     * @param depotCode 库位编码 0-全部
     * @return
     */
    @Override
    public IPage<StockCountResponse> queryStockInfo(Page<StockCountResponse> queryPage, String searchParam, String pharmCode, String zoneCode, String depotCode) {
        return pharmStockMapper.queryStockInfo(queryPage,searchParam,pharmCode,zoneCode,depotCode);
    }

    /**
     * 库存信息统计
     *
     * @param searchParam 模糊搜索(遗体名称，拼音码，五笔码)
     * @param pharmCode   仓库编码
     * @param zoneCode    分区编码
     * @param depotCode 库位编码
     * @return
     */
    @Override
    public Map stockCount(String searchParam, String pharmCode, String zoneCode, String depotCode) {
        return pharmStockMapper.stockCount(searchParam,pharmCode,zoneCode,depotCode);
    }

    /**
     * 更新库存记录
     * @param pharmStock
     * @return
     */
    @Override
    public PharmStock modify(PharmStock pharmStock) {
        notNull(pharmStock,"库存记录不能为空");
        notNull(pharmStock.getId(),"唯一id不能为空");

        if(this.mapper.updateById(pharmStock) > 0){
            return pharmStock;
        }
        throw new ServiceException("更新失败");
    }

    /**
     * 清空库存记录出库信息
     * @param remainsCode 遗体编码
     */
    @Override
    public void emptyOut(String remainsCode) {
        notNull(remainsCode,"遗体编码不能为空");

        //获取库存信息
        PharmStock pharmStock = this.queryByRemainsCode(remainsCode);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq(PharmStock.ID, pharmStock.getId());
        updateWrapper.set(PharmStock.out_oper_person, null);
        updateWrapper.set(PharmStock.out_datetime, null);
        this.mapper.update(null,updateWrapper);
    }

    /**
     * 新增
     * @param pharmStock
     * @return
     */
    @Override
    public PharmStock add(PharmStock pharmStock) {
        notNull(pharmStock,"库存信息不能为空");
        notNull(pharmStock.getRemainsCode(),"遗体编码不能为空");
        notNull(pharmStock.getPharmCode(),"仓库编码不能为空");
        notNull(pharmStock.getZoneCode(),"分区编码不能为空");
        notNull(pharmStock.getDepotCode(),"库位编码不能为空");
        notNull(pharmStock.getInPharmCode(),"入库单据编码不能为空");

        preCreate(pharmStock);

        save(pharmStock);

        return pharmStock;
    }
}
